package com.algorithms.arrays;

import com.algorithms.utils.AlgorithmException;

public class Stack<T> extends AbstractArray<T> {
	private int top = -1;

	public Stack(int maxElementCount) {
		super(maxElementCount);
	}
	
	@Override
	public T pop() throws AlgorithmException {
		if (isEmpty()){
			throw new AlgorithmException("stack is empty.");
		}else{
			return this.data[top--];
		}
	}

	@Override
	public void push(T value) throws AlgorithmException {
		if (isFull()){
			throw new AlgorithmException("stack is full.");
		}else{
			this.data[++top] = value;
		}
	}

	@Override
	public T peak() {
		return this.data[top];
	}

	@Override
	public boolean isEmpty() {
		return top == INVALID_INDEX;
	}
	
	@Override
	public boolean isFull() {
		return top == this.maxElementCount - 1;
	}
	
}
